home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / firstwebforms / listcontrolsform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  5.8 KB  |  140 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3. Imports System.Data.SqlClient
  4.  
  5. ' This form demonstrates how to leverage several features
  6. ' of List controls
  7.  
  8. Public Class ListControlsForm
  9.     Inherits System.Web.UI.Page
  10.     Protected WithEvents lstPublishers As System.Web.UI.WebControls.ListBox
  11.     Protected WithEvents lblListBoxInfo As System.Web.UI.WebControls.Label
  12.     Protected WithEvents cblContinents As System.Web.UI.WebControls.CheckBoxList
  13.     Protected WithEvents rblWeekdays As System.Web.UI.WebControls.RadioButtonList
  14.     Protected WithEvents lblDropDownListInfo As System.Web.UI.WebControls.Label
  15.     Protected WithEvents lblCheckBoxListInfo As System.Web.UI.WebControls.Label
  16.     Protected WithEvents lblRadioButtonListInfo As System.Web.UI.WebControls.Label
  17.     Protected WithEvents ddlColors As System.Web.UI.WebControls.DropDownList
  18.  
  19. #Region " Web Form Designer Generated Code "
  20.  
  21.     'This call is required by the Web Form Designer.
  22.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  23.  
  24.     End Sub
  25.  
  26.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  27.         'CODEGEN: This method call is required by the Web Form Designer
  28.         'Do not modify it using the code editor.
  29.         InitializeComponent()
  30.     End Sub
  31.  
  32. #End Region
  33.  
  34.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35.         'Put user code to initialize the page here
  36.  
  37.         If Not Page.IsPostBack Then
  38.             ' Fill a string array with the names of all known colors.
  39.             Dim colors() As String = [Enum].GetNames(GetType(System.Drawing.KnownColor))
  40.             ' Bind it to the ddlColors dropdownlist control.
  41.             ddlColors.DataSource = colors
  42.  
  43. #Const USE_DATATEXTFORMATSTRING = False
  44.  
  45. #If USE_DATATEXTFORMATSTRING = False Then
  46.             ' Open a connection to Biblio.mdb and create a DataReader.
  47.             Dim cn As New OleDbConnection(BiblioConnString)
  48.             Dim cmd As New OleDbCommand("SELECT PubId,Name FROM Publishers", cn)
  49.             cn.Open()
  50.             Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
  51.             ' Bind the DataReader to the ListBox control.
  52.             lstPublishers.DataSource = dr
  53.             lstPublishers.DataTextField = "Name"
  54.             lstPublishers.DataValueField = "PubId"
  55. #Else
  56.             ' Open a connection to Pubs and create a DataReader.
  57.             Dim cn As New OleDbConnection(OledbPubsConnString)
  58.             Dim cmd As New OleDbCommand("SELECT * FROM Titles", cn)
  59.             cn.Open()
  60.             Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
  61.             ' Bind the DataReader to the ListBox control.
  62.             lstPublishers.DataSource = dr
  63.             lstPublishers.DataTextField = "pubdate"
  64.             lstPublishers.DataValueField = "title_id"
  65.             lstPublishers.DataTextFormatString = "{0:D}"
  66. #End If
  67.  
  68.             ' Creating the collection of radio buttons via code.
  69.             Dim i As Integer
  70.             For i = 1 To 7
  71.                 Dim li As New ListItem(WeekdayName(i), i.ToString)
  72.                 rblWeekdays.Items.Add(li)
  73.             Next
  74.  
  75.             ' Create a HashTable with continent names.
  76.             Dim continents As New Hashtable(5)
  77.             continents.Add("America", 1)
  78.             continents.Add("Europa", 2)
  79.             continents.Add("Asia", 3)
  80.             continents.Add("Africa", 4)
  81.             continents.Add("Australia", 5)
  82.  
  83.             ' Bind it to the cblContinents CheckBoxList control.
  84.             cblContinents.DataSource = continents
  85.             cblContinents.DataTextField = "Key"
  86.             cblContinents.DataValueField = "Value"
  87.  
  88.             ' Perform binding for all the controls in the page.
  89.             Me.DataBind()
  90.  
  91.             ' Close the DataReader (and the Connection.)
  92.             dr.Close()
  93.         End If
  94.  
  95.     End Sub
  96.  
  97.     ' a new item in the multiselect ListBox control has been selected
  98.  
  99.     Private Sub lstPublishers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstPublishers.SelectedIndexChanged
  100.         Dim li As ListItem, msg As String
  101.         ' check the Selected property of each element in the list
  102.         For Each li In lstPublishers.Items
  103.             If li.Selected Then
  104.                 msg &= String.Format("Item = {0}, Value = {1} <br>", li.Text, li.Value)
  105.             End If
  106.         Next
  107.         lblListBoxInfo.Text = msg
  108.     End Sub
  109.  
  110.     ' an element of the DropDownList control has been selected
  111.  
  112.     Private Sub ddlColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlColors.SelectedIndexChanged
  113.         With ddlColors.SelectedItem
  114.             lblDropDownListInfo.Text = "Item = " & .Text & ", Value = " & .Value
  115.         End With
  116.     End Sub
  117.  
  118.     ' an element of the RadioButtonList control has been selected
  119.  
  120.     Private Sub rblWeekdays_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblWeekdays.SelectedIndexChanged
  121.         With rblWeekdays.SelectedItem
  122.             lblRadioButtonListInfo.Text = "Item = " & .Text & ", Value = " & .Value
  123.         End With
  124.     End Sub
  125.  
  126.     ' an element of the CheckBoxList control has been selected
  127.  
  128.     Private Sub cblContinents_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cblContinents.SelectedIndexChanged
  129.         Dim li As ListItem, msg As String
  130.         ' check the Selected property of each item
  131.         For Each li In cblContinents.Items
  132.             If li.Selected Then
  133.                 msg &= String.Format("Item = {0}, Value = {1}<br>", li.Text, li.Value)
  134.             End If
  135.         Next
  136.         lblCheckBoxListInfo.Text = msg
  137.     End Sub
  138.  
  139. End Class
  140.